package com.hd.controller;

import com.hd.pojo.Role;
import com.hd.pojo.User;
import com.hd.service.RoleService;
import com.hd.service.UserService;
import com.hd.tools.Log;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 1 on 2017/11/15.
 */
@RestController
@RequestMapping("/sys/role")
public class RoleController {

    private String msg;

    @Resource
    private RoleService roleService;

    @Resource
    private UserService userService;

    @RequestMapping("/searchRole")
    public List<Role> searchRole(){
        return roleService.searchRole();
    }

    @RequestMapping("/getUserByRoleId")
    public List<User> getUserByRoleId(Integer roleId){
        return userService.getUserByRoleId(roleId);
    }

    @PostMapping("/add")
    public String addRole(Role role){
        try {
            roleService.addRole(role);
            msg = "添加角色成功!";
        }catch (RuntimeException ex){
            msg = "添加角色失败";
            Log.logger.error(msg,ex);
        }
        return msg;
    }

    @PostMapping("/update")
    public String updateRole(Role role){
        try {
            roleService.updateRole(role);
            msg = "角色修改成功!";
        }catch (RuntimeException ex){
            msg = "角色修改失败!";
            Log.logger.error(msg,ex);
        }
        return msg;
    }

    /*删除角色*/
    @GetMapping("/deleteRole")
    public String deleteRole(Integer roleId){
        try {
            roleService.deleteRole(roleId);

            msg = "角色修改成功!";
        }catch (RuntimeException ex){
            msg = "角色修改失败!";
            Log.logger.error(msg,ex);
        }
        return msg;
    }
}
